import util from './util'
import Cookie from 'js-cookie'
export default ({ app, store }) => {
  app.router.beforeEach((to, from, next) => {
    // 初始化log数据
		if (!localStorage.getItem('initLog')) {
			localStorage.setItem('initLog','initLog')
			localStorage.setItem('uuid',util.getUuid())
			localStorage.setItem('uuidSession',util.getUuid())
			localStorage.setItem('step',0)
			localStorage.setItem('sessionTimeStamp',new Date().getTime())
		}
		// 操作间隔时间大于30分钟则更新 uuidSession(会话uuid),重置 step
		if ((new Date().getTime() - localStorage.getItem('sessionTimeStamp')) / 1000 / 60 > 30) {
			localStorage.setItem('uuidSession', util.getUuid())
			localStorage.setItem('step', 0)
			localStorage.clear('flowAnalysisLogDto')
			Cookie.remove('token')
		}
		var step = localStorage.getItem('step') * 1
		// 获取页面信息
		var pageInfo = util.getPageInfo(to)
		var flowAnalysisLogDto = {
			pageId: pageInfo.pageId,
			bizData: pageInfo.bizData,
			bizType: pageInfo.bizType,
			step: step,
			systemType: 1,
			uuid: localStorage.getItem('uuid'),
			uuidSession: localStorage.getItem('uuidSession'),
			visitType: 1,
			userId: localStorage.getItem('loginResult') ? userId: localStorage.getItem('loginResult') ? JSON.parse(localStorage.getItem('loginResult')).userId : undefined,
      prevPageId: localStorage.getItem('flowAnalysisLogDto')? JSON.parse(localStorage.getItem('flowAnalysisLogDto')).pageId : undefined
		}
		localStorage.setItem('flowAnalysisLogDto', JSON.stringify(flowAnalysisLogDto))
		if (pageInfo.pageId && (to.path != from.path || to.path==='/')) {
			step += 1
			flowAnalysisLogDto.step = step
			localStorage.setItem('step', step)
			localStorage.setItem('flowAnalysisLogDto', JSON.stringify(flowAnalysisLogDto))
			localStorage.setItem('sessionTimeStamp',new Date().getTime()) // 更新会话时间
			util.saveLog(flowAnalysisLogDto, 1)
		}
    next()
  })
 }
